package com.lsh.airticket.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dao.*;
import com.lsh.airticket.dto.RequestFlight;
import com.lsh.airticket.dto.SearchFlight;
import com.lsh.airticket.dto.ShowFlight;
import com.lsh.airticket.entity.Airroute;
import com.lsh.airticket.entity.Flight;
import com.lsh.airticket.entity.FlightInformation;
import com.lsh.airticket.entity.Ticket;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lsh
 * @date 2021/2/15
 **/
@Service
public class FlightServiceImpl implements FlightService {

    @Autowired
    FlightDao flightDao;

    @Autowired
    TicketDao ticketDao;

    @Autowired
    CityDao cityDao;

    @Autowired
    AirrouteDao airrouteDao;

    @Autowired
    AirportDao airportDao;

    @Override
    public List<ShowFlight> findFlight(SearchFlight searchFlight) {
        if (searchFlight.getTime().getTime()+1000*60*60*24 <= new Date().getTime()){
            throw new UserException("日期格式不对");
        }
        List<ShowFlight> showFlights = new ArrayList<>();
        // 根据条件查询航班
        List<FlightInformation> flights = flightDao.findFlightInformation(searchFlight);
        for (FlightInformation flight : flights) {
            ShowFlight showFlight = new ShowFlight();
            //拷贝到showFlight实体类中
            BeanUtil.copyProperties(flight, showFlight);

            String flightId = flight.getFlightId();
            List<Ticket> tickets = ticketDao.findTicketByFlight(flightId);
            //设置 机票的最小的价格。
            if (tickets == null || tickets.size() <=0) {
                break;
            }
            showFlight.setFloorPrice(tickets.get(0).getTicketPrice());

            showFlights.add(showFlight);

        }

        return showFlights;
    }

    @Override
    public PageInfo<Flight> findFlightPage() {

        List<Flight> tbFlightAll = flightDao.findTbFlightAll();
        PageInfo<Flight> pageInfo = new PageInfo<>(tbFlightAll);

        return pageInfo;
    }

    @Override
    public int insertFlight(RequestFlight requestFlight) {
        Flight flight = new Flight();
        BeanUtil.copyProperties(requestFlight, flight);
        System.out.println(cityDao.findTbCityById(requestFlight.getFromCity()));
        System.out.println(cityDao.findTbCityById(requestFlight.getToCity()));
        String fromCity = cityDao.findTbCityById(requestFlight.getFromCity()).getCityName();
        String toCity = cityDao.findTbCityById(requestFlight.getToCity()).getCityName();

        flight.setFromCity(fromCity);
        flight.setToCity(toCity);
        flight.setFlightCreatetime(new Date());
        flight.setFlightState("1");
        int i = flightDao.insertTbFlight(flight);
        return i;
    }

    @Override
    public int deleteFlightById(String flightId) {

        return flightDao.deleteTbFlightById(flightId);
    }

    @Override
    public Flight findFlightById(String flightId) {
        Flight flight = flightDao.findTbFlightById(flightId);
        Airroute airroute = airrouteDao.findTbAirrouteById(flight.getAirrouteId());
        Long fromCityId = airportDao.findTbAirportById((int) (long) airroute.getAirportUp()).getCityId();
        Long toCityId = airportDao.findTbAirportById((int) (long) airroute.getAirportDown()).getCityId();
        flight.setFromCity(fromCityId+"");
        flight.setToCity(toCityId+"");
        return flight;
    }

    @Override
    public int updateFlight(RequestFlight requestFlight) {
        Flight flight = new Flight();
        BeanUtil.copyProperties(requestFlight, flight);
        System.out.println(cityDao.findTbCityById(requestFlight.getFromCity()));
        System.out.println(cityDao.findTbCityById(requestFlight.getToCity()));
        String fromCity = cityDao.findTbCityById(requestFlight.getFromCity()).getCityName();
        String toCity = cityDao.findTbCityById(requestFlight.getToCity()).getCityName();

        flight.setFromCity(fromCity);
        flight.setToCity(toCity);
        int i = flightDao.updateTbFlight(flight);
        return i;
    }
}
